#coding=utf-8

import mysql.connector

class MysqlOper(object):
    def __init__(self):
        config = {
            'host': '127.0.0.1',
            'user': 'root',
            'password': '1234',
            'port': 3306,
            'database': 'alisql',
            'charset': 'utf8',
            'buffered': True
        }
        try:
            self.cnn = mysql.connector.connect(**config)
        except mysql.connector.Error as e:
            print('connect fails!{}'.format(e))
    
    def exec_query(self,sql,params):    
        cursor = self.cnn.cursor()
        try:
            cursor.execute(sql,params)   
            return cursor.fetchone()
        except mysql.connector.Error as e:
            print('exec_query error!{}'.format(e))
        finally:
            cursor.close()
            
    def exec_dml(self,sql,params):    
        cursor = self.cnn.cursor()
        try:
            cursor.execute(sql,params)
            self.cnn.commit()   
            return cursor.rowcount
        except mysql.connector.Error as e:
            print('exec_dml error!{}'.format(e))
            self.cnn.rollback()
            #raise e
        finally:
            cursor.close()
    
    


